package com.bangdream.michelia.view.fragment.main.task;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.SignUpBean;
import com.bangdream.michelia.presenter.ExamPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.view.activity.buy.BuyInfo;
import com.bangdream.michelia.view.adapter.TaskQrderAdapter;
import com.bangdream.michelia.view.adapter.TaskSignUpAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

/**
 * @date on 2018/8/10 23:34
 * @email 909732184@qq.com
 * 任务-报名 订单列表
 */
public class TaskOrderList extends LazyLoadFragment<ExamContract.ISignView, ExamPresenter<ExamContract.ISignView>>
        implements ExamContract.ISignView {
    private View mView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private XRefreshView refreshView;
    private TaskSignUpAdapter adapter;
    private int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.recycler_view_layout, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        refreshView = mView.findViewById(R.id.refreshView);
        refreshView.setPullLoadEnable(false);
        refreshView.setPullRefreshEnable(true);
        recyclerView = mView.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        initEmptyView(recyclerView);
        adapter = new TaskSignUpAdapter(R.layout.task_order_adapter_item);
        recyclerView.setAdapter(adapter);

        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                loadData();
            }
        });

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter a, View view, int position) {
                SignUpBean signUpBean = adapter.getData().get(position);
                String paystate = signUpBean.getPaystate();
                if(paystate!=null){
                    if(paystate.equals("0")){
                        Intent intent=new Intent(getActivity(), BuyInfo.class);
                        intent.putExtra("businessId",signUpBean.getBusinessId());
                        intent.putExtra("price",signUpBean.getCost());
                        intent.putExtra("title",signUpBean.getName());
                        intent.putExtra("type",signUpBean.getType());
                        startActivity(intent);

                    }
                }
            }
        });
    }

    @Override
    public ExamPresenter createPresenter() {
        return new ExamPresenter();
    }

    @Override
    public void loadData() {
        adapter.setEmptyView(loadingView);
        mPresenter.getSignUpList(page, PublicResources.PageSize);
    }


    @Override
    public void setSignUpList(boolean isSuccess, int page, List<SignUpBean> beans) {
        if (isSuccess && beans != null) {
            if (page == 1) {
                adapter.setNewData(beans);
                adapter.notifyDataSetChanged();
                refreshView.stopRefresh();
                //显示数据大于20条才允许上拉加载更多
                if (beans.size() > PublicResources.PageSize) {
                    refreshView.setPullLoadEnable(true);
                }
                if (beans.size() == 0) {
                    adapter.setEmptyView(noDataView);
                }
            } else {
                if (beans.size() == 0) {
                    --page;
                    showText("没有更多数据");
                }
                adapter.addData(beans);
                refreshView.stopLoadMore();
            }
        } else {
            //如果是加载新数据时失败了 则page-1
            if (page > 1) {
                refreshView.stopLoadMore();
                this.page--;
            } else {
                //还是还原为1防止出现负数
                this.page = 1;
                refreshView.stopRefresh();
                adapter.setEmptyView(isSuccess?noDataView:errorView);
            }
        }
    }
}
